package sds.cjin.schedular.activity;

import sds.cjin.schedular.activity.ColorPickerDialog.OnColorChangedListener;
import sds.cjin.schedular.innerData.additionalInfo;
import sds.cjin.schedular.innerData.classInfo;
import sds.cjin.schedular.innerData.memoInfo;
import sds.cjin.schedular.innerData.scheduleInfo;
import sds.cjin.schedular.innerData.teacherInfo;
import sds.cjin.schedular.innerData.timetable;
import sds.cjin.schedular.innerData.timezoneInfo;
import sds.cjin.schedular.innerData.todoInfo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.example.sgenteam4d.R;

class UserEnrollment extends Activity implements OnClickListener, OnColorChangedListener{

	//for activity
	int nFrame = 3, currentFrame = 1;
	int REQ_CODE_PICK_PICTURE = 0;
	Button btnNext;
	ViewFlipper flipper;
	int selectedColor = 12435678;
	
	//for save info
	timetable table;
	timezoneInfo timezone;
	additionalInfo[] additionalTimezones;
	teacherInfo teacher;
	scheduleInfo schedule;
	classInfo[] classes;
	memoInfo[] memos;
	todoInfo[] todos;
	int memoMax = 1000, todoMax = 1000, additionalTimezoneMax = 10, classMax = 100;
	
	//for save info - temporary
	Uri pictureUri;

	//for 1st frame
	ImageButton imgBtnEditPicture;
	EditText editTextUserName, editTextSubject, editTextHomeroom;
	CheckBox checkBoxIsHRT, checkBoxIcoGrade1, checkBoxIcoGrade2, checkBoxIcoGrade3;
	
	//for 2nd frame
	LinearLayout enrollmentView2;
	TextView textViewTermTitle;
	EditText editTextClassStartAtHour, editTextClassStartAtMinute, editTextStudyTimeInterval, editTextBreakTimeInterval, editTextIntervalNum, editTextLunchStartAt, editTextLunchInterval; 
	CheckBox checkBoxStartWith0;
	Button btnAddAnotherTime;
	LayoutInflater inflater;
	int nAdditional_time;
	
	//for 3rd frame
	EditText editTextScheduleTitle, editTextClassGrade, editTextClassNum;
	Button btnColorPicker, btnAddAnotherClass;
	TableLayout tableLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_enrollment_layout);
		
		//for activity
		btnNext = (Button) findViewById(R.id.btnNext);
		btnNext.setOnClickListener(this);
		flipper = (ViewFlipper) findViewById(R.id.flipper);
		
		//for 1st frame
		imgBtnEditPicture = (ImageButton) findViewById(R.id.imgBtnEditPicture);
		imgBtnEditPicture.setOnClickListener(this);
		editTextUserName = (EditText) findViewById(R.id.editTextUserName);
		checkBoxIsHRT = (CheckBox) findViewById(R.id.checkBoxIsHRT);
		checkBoxIsHRT.setOnClickListener(this);
		editTextHomeroom = (EditText) findViewById(R.id.editTextHomeroom);
		editTextSubject = (EditText) findViewById(R.id.EditTextSubject);
		checkBoxIcoGrade1 = (CheckBox) findViewById(R.id.CheckBoxIsIcoGrade1);
		checkBoxIcoGrade2 = (CheckBox) findViewById(R.id.CheckBoxIsIcoGrade2);
		checkBoxIcoGrade3 = (CheckBox) findViewById(R.id.CheckBoxIsIcoGrade3);
		
		//for 2nd frame
		enrollmentView2 = (LinearLayout) findViewById(R.id.enrollmentView2);
		textViewTermTitle = (TextView) findViewById(R.id.textViewTermTitle);
		editTextClassStartAtHour = (EditText) findViewById(R.id.editTextClassStartAtHour);
		editTextClassStartAtMinute = (EditText) findViewById(R.id.editTextClassStartAtMinute);
		editTextStudyTimeInterval = (EditText) findViewById(R.id.editTextStudyTimeInterval);
		editTextBreakTimeInterval = (EditText) findViewById(R.id.editTextBreakTimeInterval);
		editTextIntervalNum = (EditText) findViewById(R.id.EditTextIntervalNum);
		checkBoxStartWith0 = (CheckBox) findViewById(R.id.checkBoxStartWith0);
		editTextLunchStartAt = (EditText) findViewById(R.id.editTextLunchStartAt);
		editTextLunchInterval = (EditText) findViewById(R.id.editTextLunchInterval);
		btnAddAnotherTime = (Button) findViewById(R.id.btnAddAnotherTime);
		btnAddAnotherTime.setOnClickListener(this);
		inflater = getLayoutInflater();
		nAdditional_time = 1;
		
		//for 3rd frame
		editTextScheduleTitle = (EditText) findViewById(R.id.EditTextScheduleTitle);
		editTextClassGrade = (EditText) findViewById(R.id.editTextClassGrade);
		editTextClassNum = (EditText) findViewById(R.id.editTextClassNum);
		btnColorPicker = (Button) findViewById(R.id.btnColorPicker);
		btnColorPicker.setBackgroundColor(selectedColor);
		btnColorPicker.setOnClickListener(this);
		tableLayout = (TableLayout) findViewById(R.id.timetable);
		btnAddAnotherClass = (Button) findViewById(R.id.btnAddAnotherClass);
		btnAddAnotherClass.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.btnNext:
			if(currentFrame<nFrame){
				saveInfo();
				flipper.showNext();
				currentFrame++;
				changeViewValues();
			}
			else {
				//save whole info; replace below finish() to valid intent statements.
				saveInfo();
				this.finish();
			}
			break;
		case R.id.imgBtnEditPicture:
			Intent i = new Intent(Intent.ACTION_PICK);
			i.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
			i.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(i, REQ_CODE_PICK_PICTURE);
			break;
		case R.id.btnAddAnotherTime:
			LinearLayout addtional_time = (LinearLayout) inflater.inflate(R.layout.additional_time, null);
			enrollmentView2.addView(addtional_time, enrollmentView2.getChildCount() - 1);
			nAdditional_time++;
			break;
		case R.id.btnColorPicker:
			ColorPickerDialog colorPicker = new ColorPickerDialog(this, this, "selectedColor", selectedColor, selectedColor);
			colorPicker.show();
			break;
		case R.id.checkBoxIsHRT:
			if(checkBoxIsHRT.isChecked())
				editTextHomeroom.setVisibility(View.VISIBLE);
			else
				editTextHomeroom.setVisibility(View.INVISIBLE);
		}
	}
	
	public void saveInfo(){
		switch (currentFrame) {
		case 1:
			String teacherName = "googler", subject = "android";
			int homeroom = 0, grade1 = 0, grade2 = 0, grade3 = 0;
			
			if(!editTextUserName.getText().toString().equals(""))
				teacherName = editTextUserName.getText().toString();
			if(!editTextSubject.getText().toString().equals(""))
				subject = editTextSubject.getText().toString();
			if(checkBoxIsHRT.isChecked() && !editTextHomeroom.getText().toString().equals(""))
				homeroom = Integer.parseInt(editTextHomeroom.getText().toString());
			if(checkBoxIcoGrade1.isChecked())
				grade1 = 1;
			if(checkBoxIcoGrade2.isChecked())
				grade2 = 2;
			if(checkBoxIcoGrade3.isChecked())
				grade3 = 3;
			
			teacher = new teacherInfo(teacherName, subject, homeroom, grade1, grade2, grade3, pictureUri);
//			Log.d("test", "name : " + teacherName);
//			Log.d("test", "subject : " + subject);
//			Log.d("test", "homeroom : " + homeroom);
//			Log.d("test", "g1g2g3 : " + grade1+grade2+grade3);
//			Log.d("test", "pictureUri : " + pictureUri);
			break;
		case 2:
			int startHour = 8, startMinute = 0, studyTimeInterval = 50, breakTimeInterval = 10, nInterval = 8, isStartWith0 = 0, lunchStartAt = 4, lunchInterval = 60;

			if(!editTextClassStartAtHour.getText().toString().equals(""))
				startHour = Integer.parseInt(editTextClassStartAtHour.getText().toString());
			if(!editTextClassStartAtMinute.getText().toString().equals(""))
				startMinute = Integer.parseInt(editTextClassStartAtMinute.getText().toString());
			if(!editTextStudyTimeInterval.getText().toString().equals(""))
				studyTimeInterval= Integer.parseInt(editTextStudyTimeInterval.getText().toString());
			if(!editTextBreakTimeInterval.getText().toString().equals(""))
				breakTimeInterval= Integer.parseInt(editTextBreakTimeInterval.getText().toString());
			if(!editTextIntervalNum.getText().toString().equals(""))
				nInterval= Integer.parseInt(editTextIntervalNum.getText().toString());
			if(checkBoxStartWith0.isChecked())
				isStartWith0 = 1;
			if(!editTextLunchStartAt.getText().toString().equals(""))
				lunchStartAt = Integer.parseInt(editTextLunchStartAt.getText().toString());
			if(!editTextLunchInterval.getText().toString().equals(""))
				lunchInterval = Integer.parseInt(editTextLunchInterval.getText().toString());
			additionalTimezones = new additionalInfo[additionalTimezoneMax];
			additionalTimezones[0] = new additionalInfo("lunch", lunchInterval, lunchStartAt);
			for(int i = 1; i < nAdditional_time; i++){
				String tempName = "timezone"+i;
				int tempInterval = 50, tempStartAt = 1;
				LinearLayout additionalLayout = (LinearLayout) enrollmentView2.getChildAt(enrollmentView2.getChildCount() - (1 + i));
//				Log.d("test", ""+i);
//				Log.d("test", ""+additionalLayout.getChildCount());
				
				LinearLayout tempLayout = (LinearLayout) additionalLayout.getChildAt(0);
				EditText tempEditText = (EditText) tempLayout.getChildAt(1);
				if(!tempEditText.getText().toString().equals(""))
					tempName = tempEditText.getText().toString();
				
				tempLayout = (LinearLayout) additionalLayout.getChildAt(1);
				tempEditText = (EditText) tempLayout.getChildAt(1);
				if(!tempEditText.getText().toString().equals(""))
					tempStartAt = Integer.parseInt(tempEditText.getText().toString());
				
				tempLayout = (LinearLayout) additionalLayout.getChildAt(2);
				tempEditText = (EditText) tempLayout.getChildAt(1);
				if(!tempEditText.getText().toString().equals(""))
					tempInterval = Integer.parseInt(tempEditText.getText().toString());
				
				additionalTimezones[i] = new additionalInfo(tempName, tempInterval, tempStartAt);
			}
			
		//	timezone = new timezoneInfo(startHour, startMinute, studyTimeInterval, breakTimeInterval, nInterval, isStartWith0, additionalTimezones);
//			Log.d("test", "startHour : " + startHour);
//			Log.d("test", "startMinute : " + startMinute);
//			Log.d("test", "studyTimeInterval : " + studyTimeInterval);
//			Log.d("test", "breakTimeInterval" + breakTimeInterval);
//			Log.d("test", "nInterval : " + nInterval);
//			Log.d("test", "isStartWith0 : " + isStartWith0);
//			Log.d("test", "additionalTimeZones[0].name : " + additionalTimezones[0].getName());
//			Log.d("test", "additionalTimeZones[0].interval : " + additionalTimezones[0].getInterval());
//			Log.d("test", "additionalTimeZones[0].startat : " + additionalTimezones[0].getStartAt());
//			Log.d("test", "additionalTimeZones[1].name : " + additionalTimezones[1].getName());
//			Log.d("test", "additionalTimeZones[1].interval : " + additionalTimezones[1].getInterval());
//			Log.d("test", "additionalTimeZones[1].startat : " + additionalTimezones[1].getStartAt());
//			Log.d("test", "additionalTimeZones[2].name : " + additionalTimezones[2].getName());
//			Log.d("test", "additionalTimeZones[2].interval : " + additionalTimezones[2].getInterval());
//			Log.d("test", "additionalTimeZones[2].startat : " + additionalTimezones[2].getStartAt());
//			Log.d("test", "additionalTimeZones[3].name : " + additionalTimezones[3].getName());
//			Log.d("test", "additionalTimeZones[3].interval : " + additionalTimezones[3].getInterval());
//			Log.d("test", "additionalTimeZones[3].startat : " + additionalTimezones[3].getStartAt());
			break;
		case 3:
			classes = new classInfo[classMax];
			int nChildInRow = 0, cIndex = 0;
			for(int i = 0; i < tableLayout.getChildCount(); i++){
				ViewGroup curRow = (ViewGroup) tableLayout.getChildAt(i);
				nChildInRow = curRow.getChildCount();

				if(nChildInRow > 5){
					for(int j = 0; j < nChildInRow; j++){
						if(j%2!=0){
							TextView curCell = (TextView) curRow.getChildAt(j);
							if(!curCell.getText().equals("")){
								classes[cIndex] = new classInfo(curCell.getText().toString(), ((ColorDrawable)curCell.getBackground()).getColor(), i, j, 0);
								cIndex++;
							}
						}
					}
				}
			}
			
		//	schedule = new scheduleInfo(textViewTermTitle.getText().toString(), classes);
			
//			Log.d("test", classes[0].getName());
//			Log.d("test", ""+classes[0].getColor());
//			Log.d("test", ""+classes[0].getRow());
//			Log.d("test", ""+classes[0].getColumn());
//			Log.d("test", ""+classes[0].getIsEmpty());
//			Log.d("test", classes[1].getName());
//			Log.d("test", ""+classes[1].getColor());
//			Log.d("test", ""+classes[1].getRow());
//			Log.d("test", ""+classes[1].getColumn());
//			Log.d("test", ""+classes[1].getIsEmpty());
			memos = new memoInfo[memoMax];
			todos = new todoInfo[todoMax];
			
	//		table = new timetable(teacher, timezone, schedule, memos, todos);
			break;
		}		
	}
	
	public void changeViewValues(){
		
		//change each processBtn's src image
		LinearLayout processBtnLayout = (LinearLayout) findViewById(R.id.processBtns);
		for(int i = 0; i < nFrame; i++){
			ImageButton processBtn = (ImageButton) processBtnLayout.getChildAt(i);
			if(i != currentFrame - 1)
				processBtn.setImageResource(android.R.drawable.star_off);
			else
				processBtn.setImageResource(android.R.drawable.star_on);
		}
		
		//change btnNext's text
		if(currentFrame==nFrame){
			btnNext.setText("save config");
			
			for(int i = 0; i < timezone.getnInterval(); i++){
				TableRow tableRow = new TableRow(this);
				for(int j = 0; j < 5; j++){
					TextView cell = new TextView(this);
					cell.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1));
					cell.setHint("\n");
					cell.setTextColor(Color.RED);
					cell.setBackgroundColor(Color.WHITE);
					cell.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							TextView selectedView = (TextView) v;
							if(selectedView.getText().toString().equals("")){
								String temp = editTextScheduleTitle.getText().toString() + "\n" + editTextClassGrade.getText().toString() + "-" + editTextClassNum.getText().toString();
								selectedView.setText(temp);
								selectedView.setBackgroundColor(selectedColor);
							}
							else{
								selectedView.setText("");
								selectedView.setBackgroundColor(Color.WHITE);
							}
							
						}
					});
					LinearLayout blueBar = new LinearLayout(this);
					blueBar.setLayoutParams(new TableRow.LayoutParams(2, TableRow.LayoutParams.MATCH_PARENT));
					blueBar.setBackgroundColor(Color.BLUE);
					tableRow.addView(blueBar);
					tableRow.addView(cell);
				}
				LinearLayout blueBarBig = new LinearLayout(this);
				blueBarBig.setLayoutParams(new TableRow.LayoutParams(5, TableRow.LayoutParams.MATCH_PARENT));
				blueBarBig.setBackgroundColor(Color.BLUE);
				tableRow.addView(blueBarBig);
				
				LinearLayout blackBar = new LinearLayout(this);
				blackBar.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 2));
				blackBar.setBackgroundColor(Color.BLACK);
				tableLayout.addView(blackBar);
				tableLayout.addView(tableRow);
			}
			
			for(int i = 0; i < additionalTimezoneMax; i++){
				if(additionalTimezones[i]!=null){
					LinearLayout blackBar = new LinearLayout(this);
					blackBar.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 2));
					blackBar.setBackgroundColor(Color.BLACK);
					tableLayout.addView(blackBar, additionalTimezones[i].getStartAt());
					
					TableRow tableRow = new TableRow(this);
					LinearLayout blueBar = new LinearLayout(this);
					blueBar.setLayoutParams(new TableRow.LayoutParams(2, TableRow.LayoutParams.MATCH_PARENT));
					blueBar.setBackgroundColor(Color.BLUE);
					tableRow.addView(blueBar);
					
					TextView additionalTime = new TextView(this);
					additionalTime.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
					additionalTime.setText(additionalTimezones[i].getName());
					additionalTime.setTextColor(Color.MAGENTA);
					tableRow.addView(additionalTime, new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1));
					
					LinearLayout blueBarBig = new LinearLayout(this);
					blueBarBig.setLayoutParams(new TableRow.LayoutParams(5, TableRow.LayoutParams.MATCH_PARENT));
					blueBarBig.setBackgroundColor(Color.BLUE);
					tableRow.addView(blueBarBig);
					
					tableLayout.addView(tableRow, additionalTimezones[i].getStartAt()+1);
				}
			}
			
			LinearLayout blackBarBig = new LinearLayout(this);
			blackBarBig.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 5));
			blackBarBig.setBackgroundColor(Color.BLACK);
			tableLayout.addView(blackBarBig);
		}
	}
	
	//edit profile picture
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQ_CODE_PICK_PICTURE)
			if (resultCode == Activity.RESULT_OK){
				pictureUri = data.getData();
				imgBtnEditPicture.setImageURI(pictureUri);
			}
	}

	@Override
	public void colorChanged(String key, int color) {
		// TODO Auto-generated method stub
		int r = (color >> 16) & 0xFF;
		int g = (color >> 8) & 0xFF;
		int b = (color >> 0) & 0xFF;
//		Log.d("test", "r : " + r);
//		Log.d("test", "g : " + g);
//		Log.d("test", "b : " + b);
		selectedColor = color;
		btnColorPicker.setBackgroundColor(selectedColor);
	}
}
